//
//  MovieDetailViewController.m
//  豆瓣
//
//  Created by dllo on 15/12/9.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "MovieDetailView.h"
#import "AppTools.h"
#import "AFNetworking.h"
#import "LoginViewController.h"
#import "DataBaseSingleton.h"

@interface MovieDetailViewController ()
@property (nonatomic, retain) MovieDetailView *movieDetailV;
@property (nonatomic, retain) NSMutableArray *allArray;

@end

@implementation MovieDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.movie.movieName;

    [self afnGetRequest];
    
//    self.movieDetailV.movie = self.movie;
    
    
//    [AppTools getDataFromJsonWithFileName:[@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=" stringByAppendingString:self.movie.movieId] Block:^(id result) {
//        NSMutableDictionary *dic = [result objectForKey:@"result"];
//        
//
////        for (NSDictionary *dic in ) {
//            Movie *movie = [[Movie alloc] init];
//            [movie setValuesForKeysWithDictionary:dic];
////            [self.allArray addObject:movie];
//
//        self.movieDetailV.movie = movie;
//        
//        [movie release];
////        }
//        
//        
//        
//    } error:^(NSError *error) {
//        
//        
//    }];
    
    UIImage *rightImage = [[UIImage imageNamed:@"btn_nav_share"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    
    
    UIImage *leftImage = [[UIImage imageNamed:@"btn_nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    
    
    self.movieDetailV = [[MovieDetailView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:_movieDetailV];
    
}

-(void)afnGetRequest
{
    NSString *str = [@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=" stringByAppendingString:self.movieId];
    NSString *url_string = str;
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    //请求管理者 所有的网络请求,均有manager发起
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"=====%d", [NSThread isMainThread]);
        // 回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            
        });
        //        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        self.allArray = [NSMutableArray array];
        
//        NSDictionary *dic1 = responseObject;
        
        NSMutableDictionary *dic = [responseObject objectForKey:@"result"];
        
        
        //        for (NSDictionary *dic in ) {
        Movie *movie = [[Movie alloc] init];
        [movie setValuesForKeysWithDictionary:dic];
        //            [self.allArray addObject:movie];
        
        self.movieDetailV.movie = movie;
        
        [movie release];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
}


// 右边收藏按钮点击事件方法
- (void)rightBarButtonAction:(UIBarButtonItem *)button{
    // 判断登陆状态
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"BOOL"];
    if ([string isEqualToString:@"yes"]) {
        
        NSMutableArray *array = [[DataBaseSingleton shareDataBase] selectMovie];
        NSMutableArray *nameArray = [NSMutableArray array];
        for (Movie *movie in array) {
            [nameArray addObject:movie.movieName];
        }
        
        if ([nameArray containsObject:self.movie.movieName]) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *acAc = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            [alertC addAction:acAc];
            
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];

        }else {
        // 插入收藏的数据
        [[DataBaseSingleton shareDataBase] insertMovieIntoTable:self.movie];
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *acAc = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alertC addAction:acAc];
        
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        }

    }else if ([string isEqualToString:@"no"]){
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
    }
    
    
    
}


- (void)leftBarButtonAction:(UIBarButtonItem *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
